{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.02463672e95e9a83036c81a4e593b3da.315360000.1918886256.282335-22869548', 'expires_in': 2592000, 'session_key': '9mzdD0L28bUFiyArrlppYZMTJFthzMUcxkTBEXWa0Xx5rNYE0+XNK6y6BhP+rWrJy9Q1ozE5+OT+MCeV8zwfFWt2JgXPMQ==', 'access_token': '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': '01fed1bf12ccb01d24a4a57b1afac680'}\n"
     ]
    }
   ],
   "source": [
    "# 1.导入模块，输入百度官网给出的API Key和Secret Key\n",
    "\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的APIKey，client_secret为官网获取的Secret Key\n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'\n",
    "client_id = \"8YIS4b9PawXz9wjniTDeh5HZ\"\n",
    "client_secret = \"1cnF87p0NyYVLMmWxgfhrxHMkifu0p9k\"\n",
    "response = requests.get(host.format(client_id, client_secret))\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.02463672e95e9a83036c81a4e593b3da.315360000.1918886256.282335-22869548',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdD0L28bUFiyArrlppYZMTJFthzMUcxkTBEXWa0Xx5rNYE0+XNK6y6BhP+rWrJy9Q1ozE5+OT+MCeV8zwfFWt2JgXPMQ==',\n",
       " 'access_token': '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548',\n",
       " 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component',\n",
       " 'session_secret': '01fed1bf12ccb01d24a4a57b1afac680'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (让它排列得整齐一点)\n",
    "results = response.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 2015545754555,\n",
       " 'timestamp': 1603526426,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': 'ae407fb0ae5f4efe58f3d0fac00a139b',\n",
       "    'location': {'left': 271.97,\n",
       "     'top': 517.34,\n",
       "     'width': 758,\n",
       "     'height': 715,\n",
       "     'rotation': 2},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -27.84, 'pitch': 15.83, 'roll': 7.57},\n",
       "    'face_shape': {'type': 'heart', 'probability': 0.51},\n",
       "    'face_type': {'type': 'human', 'probability': 0.97}}]}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.1人脸检测与属性分析\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "#添加人脸图片的url\n",
    "params = \"{\\\"image\\\":\\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603536477084&di=5703c5fb651da5201f10ad840ea7375b&imgtype=0&src=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0310%2Fc63836a2j00q6z2bw00acc000z901bjm.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "\n",
    "access_token = '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 9415001455500,\n",
       " 'timestamp': 1603526586,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': 'ea1f2d40cd38038fcbbcfaebac4698e1',\n",
       "    'location': {'left': 167.59,\n",
       "     'top': 147.95,\n",
       "     'width': 144,\n",
       "     'height': 137,\n",
       "     'rotation': -2},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': 6.1, 'pitch': 10.96, 'roll': -5.92},\n",
       "    'face_shape': {'type': 'round', 'probability': 0.46},\n",
       "    'face_type': {'type': 'human', 'probability': 1}}]}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.2 获取第二张人脸的face_token \n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "#添加人脸图片的url\n",
    "params = \"{\\\"image\\\":\\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603536654523&di=fb80a33087a34326b30abf704734f4af&imgtype=0&src=http%3A%2F%2Fugc.hitv.com%2Fzhuanlan%2F20200228143906_6e6734bc-914e-4206-af77-5ab8ac77a5c1.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "\n",
    "access_token = '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1018410145252, 'timestamp': 1603526714, 'cached': 0, 'result': {'score': 94.4076004, 'face_list': [{'face_token': 'ae407fb0ae5f4efe58f3d0fac00a139b'}, {'face_token': 'ea1f2d40cd38038fcbbcfaebac4698e1'}]}}\n"
     ]
    }
   ],
   "source": [
    "# 3.人脸对比\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"ae407fb0ae5f4efe58f3d0fac00a139b\\\", \\\"image_type\\\": \\\"FACE_TOKEN\\\"}, {\\\"image\\\": \\\"ea1f2d40cd38038fcbbcfaebac4698e1\\\", \\\"image_type\\\": \\\"FACE_TOKEN\\\"}]\"\n",
    "\n",
    "access_token = '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
